K-Med?ides

library(cluster)
x <- rbind(matrix(rnorm(100, mean = 0.5, sd = 4.5), ncol = 2), matrix(rnorm(100, mean = 0.5, sd = 0.1), ncol = 2))
colnames(x) <- c("x", "y")

modelo1 <- pam(x, 2)
modelo2 <- kmeans(x, 2)

par(mfrow=c(1,2))

plot(x, col = modelo1$clustering, main="Clusters k-Medoids")
points(modelo1$medoids, col = 1:3, pch = 10, cex = 4)

plot(x, col = modelo2$cluster, main="Clusters k-Means") 
points(modelo2$centers, col = 1:3, pch = 10, cex = 4)

Dendogramas

set.seed(1909)
obs_centro = 5
nclusters = 3
populacao = nclusters * obs_centro
x <- rnorm(15, mean = rep(1:3, each = 5), sd = 0.2)
y <- rnorm(15, mean = rep(c(1, 2), each = 5), sd = 0.2)
mydata <- data.frame(x, y)
plot(x, y, col = "blue", pch = 19, cex = 1)
text(x + 0.05, y + 0.05, labels = as.character(1:populacao))

C?lculo das dist?ncias

dist(mydata[1:7,])
##            1          2          3          4          5          6
## 2 0.21163311                                                       
## 3 0.20633407 0.24473069                                            
## 4 0.51162859 0.54935746 0.70939918                                 
## 5 0.26365620 0.05969602 0.25564225 0.59896768                      
## 6 1.46052153 1.38486000 1.25575323 1.93301345 1.33966001           
## 7 1.30299464 1.20863745 1.10235796 1.75796875 1.16012678 0.21121056
dist(mydata[1:7,], method = "manhattan")
##            1          2          3          4          5          6
## 2 0.26622548                                                       
## 3 0.28832187 0.31114759                                            
## 4 0.63618675 0.77293885 0.92450862                                 
## 5 0.30804477 0.07333741 0.35296688 0.84627627                      
## 6 2.06381204 1.92705994 1.77549017 2.69999879 1.85372253           
## 7 1.83920285 1.70245075 1.55088098 2.47538960 1.62911334 0.22460919

Criando o Modelo de Clusterizacao Hierarquica

modelo.hc <- hclust( dist(mydata) )

Plotar o Dendograma

plot(modelo.hc,
     main="Dendrograma simples",
     xlab="Observações", sub="", ylab = "Distância")

Dendograma Utilizando o Plotly

library("plotly")
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
modelo.dendro <- as.dendrogram(modelo.hc)
plot_dendro(modelo.dendro, xmin = -0.05 ) %>% 
hide_legend() 
plot(dendextend::as.phylo.dendrogram(modelo.hc), type = "fan", label.offset = 0.05, cex = 1.2)

Mapa de Calor com Dendograma

set.seed(12345)
dados <- matrix(rnorm(400), nrow = 40)
heatmap(dados )